package com.bulletelc.apollopre.framework;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bulletelc.apollopre.utils.LogUtil;

/**
 * author: lhl
 * date  : 2017/11/15
 * desc  : MVP架构的Fragment基类,预防内存泄漏；使用到MVP架构的Fragment都继承于此类
 *
 * 下面注释的“一般写法"和"Mosby项目写法",代表两种不同创建presenter和detachView()地方
 */

public abstract class MVPBaseFragment<V extends IView,P extends IPresenter<V>> extends BaseFragment{

    protected P presenter;

    //一般写法
//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        presenter = createPresenter();
//        presenter.attachView((V) this);
//    }

    //一般写法
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        presenter.detachView(getRetainInstance());
//    }

    //Mosby项目写法
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        LogUtil.i("执行");
//        if (presenter==null) {
//            presenter = createPresenter();
//        }
//        presenter.attachView((V) this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (presenter==null) {
            presenter = createPresenter();
        }
        presenter.attachView((V) this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    //Mosby项目写法
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.detachView(getRetainInstance());
    }
    /**
     * 创建Presenter对象
     * @return
     */
    protected abstract P createPresenter();

}
